package com.framework.bdf4j.sysadm.controller;

import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.framework.bdf4j.annotations.SysLog;
import com.framework.bdf4j.comm.util.R;
import com.framework.bdf4j.sysadm.entity.SysPublicParam;
import com.framework.bdf4j.sysadm.service.SysPublicParamService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;

/**
 * 公共参数
 *
 */
@Controller
@AllArgsConstructor
@RequestMapping("/admin/param")
@Api(value = "param", tags = "公共参数配置")
public class SysPublicParamController {

    private final SysPublicParamService sysPublicParamService;

    // 跳转到公共参数配置列表页
    @GetMapping("/index")
    public String goRole() {
        return "sysadm/param/paramList";
    }

    /**
     * 分页查询
     *
     * @param page
     *            分页对象
     * @param sysPublicParam
     *            公共参数
     * @return
     */
    @ApiOperation(value = "分页查询", notes = "分页查询")
    @GetMapping("/page")
    @ResponseBody
    public R getSysPublicParamPage(Page page, SysPublicParam sysPublicParam) {
        return R.ok(sysPublicParamService.page(page, Wrappers.query(sysPublicParam)));
    }

    /**
     * 参数状态修改
     */
    @SysLog("修改参数状态")
    @ResponseBody
    @PostMapping("/changeStatus")
    @PreAuthorize("@pms.hasPermission('admin_syspublicparam_edit')")
    public R changeStatus(SysPublicParam param) {
        return R.ok(sysPublicParamService.updateById(param));
    }

    // 跳转到参数编辑页
    @GetMapping("/goEditParam")
    public String goEditParam(@RequestParam(required = false) String id, @RequestParam String oper, Model model) {
        SysPublicParam param = new SysPublicParam();
        if (!"add".equals(oper)) {
            param = sysPublicParamService.getById(Integer.parseInt(id));
        }
        model.addAttribute("viewobject", param);
        model.addAttribute("oper", oper);
        return "sysadm/param/paramEdit";
    }

    /**
     * 新增公共参数
     *
     * @param sysPublicParam
     *            公共参数
     * @return R
     */
    @ApiOperation(value = "新增公共参数", notes = "新增公共参数")
    @SysLog("新增公共参数")
    @PostMapping
    @ResponseBody
    @PreAuthorize("@pms.hasPermission('admin_syspublicparam_add')")
    public R save(SysPublicParam sysPublicParam) {
        return R.ok(sysPublicParamService.save(sysPublicParam));
    }

    /**
     * 修改公共参数
     *
     * @param sysPublicParam
     *            公共参数
     * @return R
     */
    @ApiOperation(value = "修改公共参数", notes = "修改公共参数")
    @SysLog("修改公共参数")
    @PutMapping
    @ResponseBody
    @PreAuthorize("@pms.hasPermission('admin_syspublicparam_edit')")
    public R updateById(SysPublicParam sysPublicParam) {
        return sysPublicParamService.updateParam(sysPublicParam);
    }

    /**
     * 通过id删除公共参数
     *
     * @param publicId
     *            id
     * @return R
     */
    @ApiOperation(value = "删除公共参数", notes = "删除公共参数")
    @SysLog("删除公共参数")
    @ResponseBody
    @DeleteMapping("/{publicId}")
    @PreAuthorize("@pms.hasPermission('admin_syspublicparam_del')")
    public R removeById(@PathVariable Long publicId) {
        return sysPublicParamService.removeParam(publicId);
    }

}
